<template>
  <div class="overseas-in">
    <el-form :model="queryForm" label-position="right" class="class-form">
      <el-row :gutter="20">
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
          <el-form-item label="Sku">
            <el-input
              v-model="queryForm.sku"
              clearable
              placeholder="Sku"
              prefix-icon="el-icon-search"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
          <el-form-item :label="$t('query.开始结束时间')">
            <el-date-picker
              v-model="queryForm.date"
              type="daterange"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              style="width: 100%"
              :start-placeholder="$t('query.开始时间')"
              :end-placeholder="$t('query.结束时间')"
              clearable
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
          <el-form-item label-width="0px">
            <el-button type="primary" @click="getDataList">{{
              $t("form.queryBtn")
            }}</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="table-container">
      <el-table border :data="tableData">
        <el-table-column prop="id" label="id" align="center" minWidth="200">
        </el-table-column>
        <el-table-column
          prop="sku"
          :label="$t('overseas_in.商品sku')"
          align="center"
          minWidth="200"
        >
        </el-table-column>
        <el-table-column
          prop="product"
          :label="$t('overseas_in.商品墨西哥名')"
          align="center"
          minWidth="200"
        >
        </el-table-column>
        <el-table-column
          prop="total"
          :label="$t('overseas_in.商品数量')"
          align="center"
          minWidth="100"
        >
        </el-table-column>
        <el-table-column
          prop="state"
          :label="$t('overseas_in.商品状态')"
          align="center"
          minWidth="100"
        >
          <template slot-scope="scope">
            {{
              scope.row.state
                ? scope.row.state == "N"
                  ? $t("query.否")
                  : $t("query.是")
                : "-"
            }}
          </template>
        </el-table-column>
        <el-table-column
          prop="Unusual"
          :label="$t('overseas_in.异常商品数量')"
          align="center"
          minWidth="100"
        >
        </el-table-column>
        <el-table-column
          prop="OutDate"
          :label="$t('overseas_in.发货日期')"
          align="center"
          minWidth="200"
        >
        </el-table-column>
        <el-table-column
          prop="ReciveDate"
          :label="$t('overseas_in.到货日期')"
          align="center"
          minWidth="200"
        >
        </el-table-column>
        <el-table-column
          prop="LastDate"
          :label="$t('overseas_in.最后修改日期')"
          align="center"
          minWidth="200"
        >
        </el-table-column>
        <el-table-column
          prop="remark"
          :label="$t('overseas_in.备注')"
          align="center"
          minWidth="200"
        >
        </el-table-column>
        <el-table-column
          prop="person"
          :label="$t('overseas_in.修改人')"
          align="center"
          minWidth="100"
        >
        </el-table-column>
        <el-table-column
          :label="$t('overseas_in.操作')"
          align="center"
          fixed="right"
          width="100"
        >
          <template slot-scope="scope">
            <el-button
              @click="handleClick(scope.row)"
              type="text"
              size="small"
              >{{ $t("form.editBtn") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.start"
        background
        :page-sizes="[10, 100, 200, 300, 400]"
        :page-size="page.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      width="600px"
      title="编辑"
      top="50px"
      :close-on-click-modal="false"
      :visible.sync="modelShow"
    >
      <el-form :model="editForm" label-position="top">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item :label="$t('overseas_in.商品sku')">
              <el-input
                v-model="editForm.sku"
                autocomplete="off"
                readonly
                :placeholder="$t('overseas_in.商品sku')"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('overseas_in.商品中文名')">
              <el-input
                v-model="editForm.product"
                readonly
                :placeholder="$t('overseas_in.商品中文名')"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('overseas_in.商品数量')">
              <el-input-number
                v-model="editForm.total"
                :min="1"
                :disabled="true"
                :placeholder="$t('overseas_in.商品数量')"
                label="商品总数"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('overseas_in.异常商品数量')">
              <el-input-number
                v-model="editForm.Unusual"
                :min="1"
                :placeholder="$t('overseas_in.异常商品数量')"
                label="异常商品数量"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('overseas_in.到货日期')">
              <el-date-picker
                v-model="editForm.ReciveDate"
                type="date"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                style="width: 100%"
                :placeholder="$t('overseas_in.到货日期')"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('overseas_in.商品状态')">
              <el-select
                style="width: 100%"
                v-model="editForm.state"
                :placeholder="$t('overseas_in.商品状态')"
              >
                <el-option :label="$t('query.在售')" value="Y"> </el-option>
                <el-option :label="$t('query.不在售')" value="N"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('overseas_in.备注')">
              <el-input
                v-model="editForm.remark"
                :placeholder="$t('overseas_in.备注')"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('overseas_in.修改人')">
              <el-input
                v-model="editForm.person"
                :placeholder="$t('overseas_in.修改人')"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="modelShow = false">{{
          $t("form.cancleBtn")
        }}</el-button>
        <el-button type="primary" :loading="loading" @click="submit">{{
          $t("form.sureBtn")
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { cloneDeep } from "loadsh";
import { MxReciveSelectApi, MxReciveDataApi, MxReciveUpApi } from "@/api";
export default {
  name: "overseas-in",
  data() {
    return {
      queryForm: {
        sku: "",
        date: [],
        StartDate: "",
        EndDate: "",
      },
      editForm: {},
      tableData: [],
      page: {
        start: 1,
        limit: 10,
      },
      total: 0,
      modelShow: false,
      loading: false,
    };
  },
  watch: {
    "queryForm.date": {
      handler(value) {
        if (!value) {
          this.queryForm.StartDate = "";
          this.queryForm.EndDate = "";
        }
      },
    },
  },
  created() {
    this.getDataList();
  },
  methods: {
    handleSizeChange(e) {
      this.page.limit = e;
      this.getDataList();
    },
    handleCurrentChange(e) {
      this.page.start = e;
      this.getDataList();
    },
    async getDataList() {
      let params = cloneDeep({
        ...this.page,
        start: this.page.start - 1,
      });

      if (this.queryForm.date) {
        this.queryForm.StartDate = this.queryForm.date[0];
        this.queryForm.EndDate = this.queryForm.date[1];
      }
      Object.keys(this.queryForm).map((key) => {
        if (this.queryForm[key] && key != "date") {
          params[key] = this.queryForm[key];
        }
      });
      const { data, total } = await MxReciveSelectApi(params);
      this.tableData = data;
      this.total = total;
    },
    handleClick(row) {
      console.log(row);
      this.editForm = cloneDeep(row);
      this.modelShow = true;
    },
    async submit() {
      this.loading = true;
      try {
        delete this.editForm.LastDate;
        await MxReciveUpApi(this.editForm);
        this.getDataList();
        this.$message.success("修改成功");
        this.modelShow = false;
        this.loading = false;
      } catch (error) {
        this.loading = false;
      }
    },
  },
};
</script>

<style lang="scss" scoped src="./overseas-in.scss"></style>
